import java.lang.reflect.Constructor;

/**
 * @User: vitobo
 * @Date: 2024-07-03
 * @Description: 枚举
 *
 */
public enum TestEnum {
    RED("RED", 1),BLACK("BLACK",10),GREEN("GREEN",3),
    WHITE("WHITE",8);

    private String color;
    private int ori;


    //默认是私有的
    TestEnum(String color, int ori){
        this.color = color;
        this.ori = ori;
    }

    public static TestEnum getEnumKey (int key) {
        for (TestEnum t: TestEnum.values()) {
            if(t.ori == key) {
                return t;
            }
        }
        return null;
    }
    public static void reflectPrivateConstructor() {
        try {
            Class<?> classStudent = Class.forName("TestEnum");
//注意传入对应的参数,获得对应的构造方法来构造对象,当前枚举类是提供了两个参数分别是String和int。
            Constructor<?> declaredConstructorStudent =
                    classStudent.getDeclaredConstructor(String.class,int.class,String.class,int.class);
//设置为true后可修改访问权限
            declaredConstructorStudent.setAccessible(true);
//这里为了凑齐参数，后两个参数随便给，不给也行，默认为空值
            Object objectStudent = declaredConstructorStudent.newInstance("绿色",666,"父类参数",888);
            TestEnum testEnum = (TestEnum) objectStudent;
            System.out.println("获得枚举的私有构造函数："+testEnum);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String[] args) {
        TestEnum.reflectPrivateConstructor();
    }

    public static void main2(String[] args) {
        TestEnum[] testEnum = TestEnum.values();
        for (int i = 0; i < testEnum.length; i++) {
            System.out.println(testEnum[i] +" ori: "+testEnum[i].ordinal());
        }
        System.out.println();
        System.out.println("============");

        TestEnum testEnum2 = TestEnum.valueOf("WHITE");
        System.out.println(testEnum2);
        System.out.println("============");

        System.out.println(RED.compareTo(GREEN));
        System.out.println(GREEN.compareTo(BLACK));
    }

    public static void main1(String[] args) {
        TestEnum testEnum = TestEnum.BLACK;
        switch (testEnum) {
            case RED:
                System.out.println("红色");
                break;
            case BLACK:
                System.out.println("黑色");
                break;
            case GREEN:
                System.out.println("绿色");
                break;
            case WHITE:
                System.out.println("白色");
                break;
            default:
                System.out.println("没有对应的颜色");
                break;
        }

    }

}
